package com.yunshan.member.service.impl;

import com.yunshan.common.utils.DateUtils;
import com.yunshan.member.domain.MemberSubCardInfo;
import com.yunshan.member.domain.MemberUserInfo;
import com.yunshan.member.domain.ResultMember;
import com.yunshan.member.mapper.MemberSubCardInfoMapper;
import com.yunshan.member.service.IMemberSubCardInfoService;
import com.yunshan.member.service.IMemberUserInfoService;
import com.yunshan.wechat.domain.UserInfoVo;
import com.yunshan.wechat.domain.WechatUserInfo;
import com.yunshan.wechat.service.IWechatUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 副卡信息Service业务层处理
 *
 * @author yunshan
 * @date 2025-01-17
 */
@Service
public class MemberSubCardInfoServiceImpl implements IMemberSubCardInfoService
{
    @Autowired
    private MemberSubCardInfoMapper memberSubCardInfoMapper;
    @Autowired
    private IMemberUserInfoService memberUserInfoService;
    @Autowired
    private IWechatUserInfoService wechatUserInfoService;
    /**
     * 查询副卡信息
     *
     * @param id 副卡信息主键
     * @return 副卡信息
     */
    @Override
    public MemberSubCardInfo selectMemberSubCardInfoById(Long id)
    {
        MemberSubCardInfo memberSubCardInfo = memberSubCardInfoMapper.selectMemberSubCardInfoById(id);

        //查询主卡信息
        MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoById(memberSubCardInfo.getMemberId());
        memberSubCardInfo.setMemberUserInfo(memberUserInfo);

        return memberSubCardInfo;
    }

    /**
     * 查询副卡信息列表
     *
     * @param memberSubCardInfo 副卡信息
     * @return 副卡信息
     */
    @Override
    public List<MemberSubCardInfo> selectMemberSubCardInfoList(MemberSubCardInfo memberSubCardInfo)
    {
        return memberSubCardInfoMapper.selectMemberSubCardInfoList(memberSubCardInfo);
    }

    /**
     * 新增副卡信息
     *
     * @param memberSubCardInfo 副卡信息
     * @return 结果
     */
    @Override
    public int insertMemberSubCardInfo(MemberSubCardInfo memberSubCardInfo)
    {
        memberSubCardInfo.setCreateTime(DateUtils.getNowDate());
        return memberSubCardInfoMapper.insertMemberSubCardInfo(memberSubCardInfo);
    }

    /**
     * 修改副卡信息
     *
     * @param memberSubCardInfo 副卡信息
     * @return 结果
     */
    @Override
    public int updateMemberSubCardInfo(MemberSubCardInfo memberSubCardInfo)
    {
        if (memberSubCardInfo.getStatus() != null && memberSubCardInfo.getStatus() == 1) {
            memberSubCardInfo.setIssueTime(DateUtils.getNowDate());
            //查询是否存在小程序用户，存在则绑定并删除临时会籍
            MemberSubCardInfo subCardInfo = memberSubCardInfoMapper.selectMemberSubCardInfoById(memberSubCardInfo.getId());
            if (subCardInfo != null) {
                WechatUserInfo subUserInfo = wechatUserInfoService.selectUserInfoByLoginPhone(subCardInfo.getPhone());
                if (subUserInfo != null) {
                    memberSubCardInfo.setUserId(subUserInfo.getId());

                    //删除临时会籍
                    MemberUserInfo preUserInfo = memberUserInfoService.selectUserInfoByUserId(subUserInfo.getId());
                    if (preUserInfo != null) {
                        memberUserInfoService.deleteMemberUserInfoById(preUserInfo.getId());
                    }

                    //更新用户类型
                    subUserInfo.setName(subCardInfo.getName());
                    subUserInfo.setType(2);
                    wechatUserInfoService.updateWechatUserInfo(subUserInfo);
                }
            }
        }

        memberSubCardInfo.setUpdateTime(DateUtils.getNowDate());
        return memberSubCardInfoMapper.updateMemberSubCardInfo(memberSubCardInfo);
    }

    /**
     * 批量删除副卡信息
     *
     * @param ids 需要删除的副卡信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberSubCardInfoByIds(Long[] ids)
    {
        return memberSubCardInfoMapper.deleteMemberSubCardInfoByIds(ids);
    }

    /**
     * 删除副卡信息信息
     *
     * @param id 副卡信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberSubCardInfoById(Long id)
    {
        return memberSubCardInfoMapper.deleteMemberSubCardInfoById(id);
    }

    @Override
    public int selectCountByMemberId(Long id) {
        return memberSubCardInfoMapper.selectCountByMemberId(id);
    }

    @Override
    public MemberSubCardInfo selectUserInfoByLoginPhone(String loginPhone) {
        return memberSubCardInfoMapper.selectUserInfoByLoginPhone(loginPhone);
    }

    @Override
    public ResultMember selectResultMember(Long memberId) {
        return memberSubCardInfoMapper.selectResultMember(memberId);
    }

    @Override
    public UserInfoVo selectUniUserInfo(Long userId) {
        return memberSubCardInfoMapper.selectUniUserInfo(userId);
    }

    @Override
    public MemberSubCardInfo selectUserInfoByUserId(Long userId) {
        return memberSubCardInfoMapper.selectUserInfoByUserId(userId);
    }

    @Override
    public MemberSubCardInfo selectUserInfoByPhone(String phone) {
        return memberSubCardInfoMapper.selectUserInfoByPhone(phone);
    }

    @Override
    public int updateMemberSubTotalAmount(Long memberId, BigDecimal amount) {
        return memberSubCardInfoMapper.updateMemberSubTotalAmount(memberId, amount);
    }

    @Override
    public List<MemberSubCardInfo> selectMemberSubCardStatusList(MemberSubCardInfo memberSubCardInfo) {
        return memberSubCardInfoMapper.selectMemberSubCardStatusList(memberSubCardInfo);
    }

    @Override
    public List<MemberSubCardInfo> selectMemberSubCardInfoRecords(MemberSubCardInfo memberSubCardInfo) {
        return memberSubCardInfoMapper.selectMemberSubCardInfoRecords(memberSubCardInfo);
    }
}
